
Page({
  data: {
    itemText: '',
    list: []
  },
  createItem() {
    const self = this
    if(this.data.itemText.length === 0) {
      wx.showToast({
        title: '内容不能为空',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.request({
        method: 'POST',
        url: 'https://api.yizhanketang.cn/api/v1/todos',
        data: {
          content: self.data.itemText
        },
        success(res) {
          self.init()
          self.setData({
            itemText: ''
          })
        }
      })
    }
  },
  toDetail(e) {
    // console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: `./show?id=${e.currentTarget.dataset.id}`
    })
  },
  del(e) {
    const self = this
    wx.showModal({
      title: '警告',
      content: '你要确定删除此项数据吗？',
      success (res) {
        if (res.confirm) {
          wx.request({
            method: 'POST',
            url: `https://api.yizhanketang.cn/api/v1/todos/${e.currentTarget.dataset.id}/delete`,
            success(res) {
              self.init()
            }
          })
        }
      }
    })
  },
  onShow() {
    // console.log('onShow')
    // this.init()
  },
  onLoad() {
    this.init()
  },
  init() {
    const self = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      method: 'GET',
      url: 'https://api.yizhanketang.cn/api/v1/todos',
      success(res) {
        // console.log(res)
        self.setData({
          list: res.data.data
        })
      },
      complete(res) {
        wx.hideLoading()
      }
    })
  }
})